package com.tepth.busfix.data;

import android.content.res.AssetManager;
import android.util.SparseArray;

import com.tepth.busfix.MyApplication;
import com.tepth.busfix.bean.reponse.Action;
import com.tepth.busfix.bean.reponse.BusType;
import com.tepth.busfix.bean.reponse.ChooseOperator;
import com.tepth.busfix.bean.reponse.Repair;
import com.tepth.busfix.bean.reponse.Trouble;
import com.tepth.busfix.bean.reponse.TroubleClassification;
import com.tepth.busfix.bean.reponse.TroubleContent;
import com.tepth.busfix.bean.reponse.TroubleType;
import com.tepth.busfix.network.GsonResponsePasare;
import com.tepth.busfix.tools.BeanCloneUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 张宇 on 2017/3/13.
 */

public class DataProvider {
    //故障及操作建议
    private static ArrayList<TroubleClassification> allClassifications = new ArrayList<>();
    //故障建议界面需要显示
    private static ArrayList<TroubleClassification> filterClassifications = new ArrayList<>();
    private static List<TroubleContent.Operation> OPERATION_LIST = new ArrayList<>();
    //故障维修界面的故障数据
    private static List<Trouble> troubleList = new ArrayList<>();

    public static ArrayList<TroubleClassification> getClassificatons() {
        return allClassifications;
    }

    public static List<TroubleContent.Operation> getOperationList() {
        return OPERATION_LIST;
    }

    public static void setOperationList(List<TroubleContent.Operation> operationList) {
        OPERATION_LIST.clear();
        OPERATION_LIST.addAll(operationList);
    }

    public static void setClassificatons(ArrayList<TroubleClassification> list) {
        allClassifications.clear();
        allClassifications.addAll(list);
    }

    public static void setTroubleList(List<Trouble> list) {
        troubleList.clear();
        if (list != null) {
            troubleList.addAll(list);
        }
    }

    public static List<TroubleClassification> getFilterClassifications() {
        if (filterClassifications == null) {
            filterClassifications = new ArrayList<>();
        }
        filterClassifications.clear();
        //先去重
        SparseArray<TroubleClassification> sparseArray = new SparseArray<>();
        SparseArray<List<TroubleType>> listSparseArray = new SparseArray<>();
        for (Trouble trouble : troubleList) {
            TroubleClassification classification = trouble.getClassification();
            int classificationId = (int) classification.getId();
            sparseArray.put((int) classification.getId(), classification);
            List<TroubleType> typeList = listSparseArray.get(classificationId);
            if (typeList != null) {
                typeList.add(new TroubleType(trouble.getTroubleTypeId(),
                        classificationId, classification.getName(), trouble.getCode(),
                        trouble.getName(), false, null, false));
            } else {
                typeList = new ArrayList<>();
                typeList.add(new TroubleType(trouble.
                        getTroubleTypeId(),
                        classificationId, classification.getName(), trouble.getCode(),
                        trouble.getName(), false, null, false));
                listSparseArray.put(classificationId, typeList);
            }
        }
        //数据源里面过滤出对应大类型
        int key;
        for (int i = 0; i < listSparseArray.size(); i++) {
            key = listSparseArray.keyAt(i);
            for (TroubleClassification troubleClassification : allClassifications) {
                if (key == troubleClassification.getId()) {
                    filterClassifications.add(BeanCloneUtil.cloneTo(troubleClassification));
                }
            }
        }
        //过滤出大类型里面对应的troubleType
        for (TroubleClassification troubleClassification : filterClassifications) {
            int id = (int) troubleClassification.getId();
            ArrayList<TroubleType> filterTroubleType = (ArrayList<TroubleType>) listSparseArray.get(id);
            ArrayList<TroubleType> typeList = troubleClassification.getListTroubleType();
            if (typeList != null) {
                for (TroubleType troubleType : filterTroubleType) {
                    for (TroubleType type : typeList) {
                        if (troubleType.getId() == type.getId()) {
                            troubleType.setListProcess(type.getListProcess());
                            break;
                        }
                    }
                }
            }
            troubleClassification.setListTroubleType(filterTroubleType);
        }
        return filterClassifications;
    }

    public static ArrayList<BusType> getBusType() {

        String json = getJson("busType.txt");
        return new GsonResponsePasare<ArrayList<BusType>>() {
        }.deal(json);
    }

    public static Repair getRepairInfo() {
        String json = getJson("repair.txt");
        return new GsonResponsePasare<Repair>() {
        }.deal(json);
    }

    public static List<ChooseOperator> getChooseOperator() {
        String json = getJson("choose_operator.txt");
        return new GsonResponsePasare<List<ChooseOperator>>() {
        }.deal(json);
    }

    public static List<Action> getActionList() {
        String json = getJson("action.txt");
        return new GsonResponsePasare<List<Action>>() {
        }.deal(json);
    }

    public static String getJson(String fileName) {

        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = MyApplication.getContext().getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}
